package com.scholarship.modules.admin.controller;

import com.github.pagehelper.PageInfo;
import com.scholarship.common.entity.RetCode;
import com.scholarship.common.entity.RetResponse;
import com.scholarship.common.entity.RetResult;
import com.scholarship.common.utils.FileUploadUtil;
import com.scholarship.modules.admin.dto.QueryDto;
import com.scholarship.common.dto.ResultListDto;
import com.scholarship.modules.admin.dto.StudentQueryDto;
import com.scholarship.modules.admin.entity.UserInfo;
import com.scholarship.modules.admin.exce.UpdateFailedException;
import com.scholarship.modules.admin.service.InOutFileService;
import com.scholarship.modules.admin.service.StudentInfoListService;
import com.scholarship.modules.admin.dto.StudentInfoListDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @Author Z.J.H
 * @create 2021/4/14 16:43
 */
@Api(tags = {"学生信息管理控制层"})
@RestController
@Slf4j
public class StudentInfoController {

    @Autowired
    StudentInfoListService studentInfoListService;

    /**
     * 获取学生管理列表查询条件下拉框信息
     * @return
     */
    @ApiOperation("获取学生管理列表查询条件下拉框信息(admin,backend)")
    @GetMapping({"/admin/userInfo/query","/backend/userInfo/query"})
    public RetResult getStudentQuery() {
        try {
            Map<String, List<String>> map = studentInfoListService.getStudentQuery();
            return RetResponse.makeRsp(RetCode.SUCCESS,"获取查询信息成功",map);
        }catch (Exception e) {
            log.error(e.getMessage(),e);
            return RetResponse.makeRsp(RetCode.INTERNAL_SERVER_ERROR,"未知错误");
        }
    }

    /**
     * 获取所有学生信息
     * @param pageNum 页码
     * @param pageSize 每页显示数量
     * @return
     */
    @ApiOperation("获取所有学生信息")
    @GetMapping("/admin/userInfo/all")
    public RetResult listAllStudentInfo(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) {
        try {
            ResultListDto<StudentInfoListDto> result = studentInfoListService.listAllStudentInfo(pageNum,pageSize);
            return RetResponse.makeOKRsp(result);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return RetResponse.makeErrRsp("列表信息获取失败");
        }
    }

    /**
     * 根据查询条件查询学生信息
     * @param pageNum
     * @param pageSize
     * @param studentQueryDto
     * @return
     */
    @ApiOperation("根据查询条件查询学生信息")
    @GetMapping("/admin/userInfo/list")
    public RetResult listStudentInfo(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                                     StudentQueryDto studentQueryDto) {
        try {
            ResultListDto<StudentInfoListDto> result = studentInfoListService.listStudentInfoByQuery(pageNum,pageSize,studentQueryDto);
            return RetResponse.makeOKRsp(result);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return RetResponse.makeErrRsp("列表信息获取失败");
        }
    }

    /**
     * 根据搜索查询学生信息
     * @param pageNum
     * @param pageSize
     * @param searchStr
     * @return
     */
    @ApiOperation("根据搜索查询学生信息")
    @GetMapping("/admin/userInfo/search")
    public RetResult listStudentInfo(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                                     @RequestParam("searchStr") String searchStr) {
        try {
            ResultListDto<StudentInfoListDto> result = studentInfoListService.listStudentInfoBySearch(pageNum,pageSize,searchStr);
            return RetResponse.makeOKRsp(result);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return RetResponse.makeErrRsp("列表信息获取失败");
        }
    }

    /**
     * 初始化用户密码
     * @param userId    用户id
     * @param newPwd    新密码
     * @return
     * @throws Exception
     */
    @ApiOperation("初始化学生密码")
    @PostMapping("/admin/userInfo/initPwd")
    public RetResult initUserPassword(@RequestParam(value = "userId") String userId,
                                      @RequestParam(value = "newPwd") String newPwd) throws Exception {
        try {
            studentInfoListService.changeUserPwd(userId,newPwd);
            return RetResponse.makeOKRsp();
        }catch (Exception e) {
            log.error(e.getMessage(),e);
            return RetResponse.makeRsp(RetCode.INTERNAL_SERVER_ERROR,"未知错误");
        }
    }

    /**
     * 添加新的学生信息
     * @param userInfo
     * @return
     */
    @ApiOperation("添加新的学生信息")
    @PostMapping("/admin/userInfo/add")
    public RetResult addStudentInfo(@RequestBody UserInfo userInfo) {
        try {
            if(studentInfoListService.addStudentInfo(userInfo)) {
                return RetResponse.makeRsp(RetCode.SUCCESS,"添加成功");
            }else {
                return RetResponse.makeRsp(RetCode.CONTENT_EXIST,"学生信息已存在");
            }
        }catch (Exception e) {
            log.error(e.getMessage(),e);
            return RetResponse.makeRsp(RetCode.FAIL,"未知错误");
        }
    }

    /**
     * 通过用户id获得学生基本信息
     * @param userId
     * @return
     */
    @ApiOperation("通过用户id获得学生基本信息")
    @GetMapping("/admin/userInfo/get")
    public RetResult getInfoByUserId(@RequestParam("userId") String userId) {
        try {
            UserInfo userInfo = studentInfoListService.getStudentInfo(userId);
            if(userInfo != null) {
                return RetResponse.makeRsp(RetCode.SUCCESS,"成功获取" + userId + "信息",userInfo);
            }else {
                return RetResponse.makeRsp(RetCode.USER_NOT_EXIST,"该用户不存在");
            }
        }catch (Exception e) {
            log.error(e.getMessage(),e);
            return RetResponse.makeRsp(RetCode.FAIL,"未知错误");
        }
    }

    /**
     * 修改学生基本信息
     * @param userInfo
     * @return
     */
    @ApiOperation("修改学生基本信息")
    @PostMapping("/admin/userInfo/update")
    public RetResult updateStudentInfo(@RequestBody UserInfo userInfo) {
        try {
            studentInfoListService.updateStudentInfo(userInfo);
            return RetResponse.makeRsp(RetCode.SUCCESS,"更新成功");
        }catch (Exception e) {
            log.error(e.getMessage(),e);
            return RetResponse.makeRsp(RetCode.FAIL,"未知错误");
        }
    }

    /**
     * 冻结学生账号
     * @param userId
     * @return
     */
    @ApiOperation("冻结学生账号")
    @PostMapping("/admin/userInfo/frozen")
    public RetResult frozenUser(@RequestParam("userId") String userId) {
        try {
            //studentInfoListService.frozenUserAccent(userId);
            studentInfoListService.deleteUser(userId);
            return RetResponse.makeRsp(RetCode.SUCCESS,"冻结成功");
        } catch (UpdateFailedException e) {
            log.error(e.getMessage(),e);
            return RetResponse.makeRsp(RetCode.FAIL,e.getLocalizedMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return RetResponse.makeRsp(RetCode.FAIL,"删除失败");
        }
    }

    /**
     * 解封学生账号
     * @param userId
     * @return
     */
    @ApiOperation("解封学生账号")
    @PostMapping("/admin/userInfo/activate")
    public RetResult activateUser(@RequestParam("userId") String userId) {
        try {
            studentInfoListService.activateUserAccent(userId);
            return RetResponse.makeRsp(RetCode.SUCCESS,"解封成功");
        } catch (UpdateFailedException e) {
            log.error(e.getMessage(),e);
            return RetResponse.makeRsp(RetCode.FAIL,e.getLocalizedMessage());
        }
    }

    /**
     * 根据学年和专业更新加权平均分排名
     * @param year
     * @param major
     * @return
     */
    @ApiOperation("根据学年和专业更新加权平均分排名")
    @GetMapping("/admin/userInfo/rank")
    public RetResult uploadRank(@RequestParam("year") String year,
                                @RequestParam("major") String major) {
        try {
            if(studentInfoListService.updateRankByYearMajor(year,major)) {
                return RetResponse.makeRsp(RetCode.SUCCESS,"排名更新成功");
            }else {
                return RetResponse.makeRsp(RetCode.EMPTY_CONTENT,"无该学年专业信息");
            }
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return RetResponse.makeRsp(RetCode.FAIL,"未知错误");
        }
    }

    /**
     * 更新全部学年专业的加权平均分排名
     * @return
     */
    @ApiOperation("更新全部学年专业的加权平均分排名")
    @GetMapping("/admin/userInfo/rank/all")
    public RetResult uploadAllRank() {
        try {
            studentInfoListService.updateAllRank();
            return RetResponse.makeRsp(RetCode.SUCCESS,"排名更新成功");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return RetResponse.makeRsp(RetCode.FAIL,"未知错误");
        }
    }
}
